package com.andy.blog.admin.controller;

import com.andy.blog.admin.dto.FileInfoDTO;
import com.andy.blog.admin.service.FileBusService;
import com.andy.blog.common.reponse.GeneralResponseResult;
import com.andy.blog.dal.vo.FileInfoVO;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.multipart.MultipartFile;

import javax.annotation.Resource;
import java.util.List;

/**
 * Created by andy on 2023/6/27 15:07
 */
@RestController
@RequestMapping("/file")
public class FileController {

    @Resource
    private FileBusService fileBusService;

    /**
     * 文件上传
     */
    @PostMapping("/uploadFile")
    public GeneralResponseResult<FileInfoDTO> uploadFile(@RequestParam("file") MultipartFile file, FileInfoVO fileInfoVO) {
        return GeneralResponseResult.success(fileBusService.uploadFile(file, fileInfoVO));
    }

    /**
     * 文件删除
     */
    @DeleteMapping("/deleteFile")
    public GeneralResponseResult<Void> deleteFile(@RequestParam String filePath) {
        fileBusService.deleteFile(filePath);
        return GeneralResponseResult.success();
    }

    /**
     * 文件列表
     */
    @GetMapping("/listFile")
    public GeneralResponseResult<List<FileInfoDTO>> listFiles() {
        List<FileInfoDTO> list = fileBusService.listFiles();
        return GeneralResponseResult.success(list);
    }
}
